package com.h117121.fashionshop.controller.admin;

import java.io.InputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.h117121.fashionshop.constant.AppConstant;
import com.h117121.fashionshop.core.dom.Report;
import com.h117121.fashionshop.core.tool.ReportUtil;

@Controller
@RequestMapping("/admin/report")
public class AdminReportController {
	ReportUtil reportProvider = new ReportUtil();
	List<Report> listReport = null;
	Report report = null;

	@RequestMapping(method = RequestMethod.GET)
	protected ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("AdminReportPage");
		modelAndView.addObject("currentMenu", "reportList");

		InputStream reportMapping = request.getSession().getServletContext()
				.getResourceAsStream("/WEB-INF/config/reportsMapping.xml");
		InputStream reportConfig = request
				.getSession()
				.getServletContext()
				.getResourceAsStream(
						"/WEB-INF/config/reportServerConfig.properties");
		reportProvider.loadConfig(reportConfig);

		listReport = reportProvider.getReportList(reportMapping);
		modelAndView.addObject("listReport", listReport);

		return modelAndView;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/{reportName}", method = RequestMethod.GET, params = { "type" })
	public void getReport(HttpServletRequest request,
			HttpServletResponse response,
			@PathVariable("reportName") String reportName,
			@RequestParam(value = "type", required = true) String type)
			throws Exception {
		InputStream reportMapping = request.getSession().getServletContext()
				.getResourceAsStream("/WEB-INF/config/reportsMapping.xml");
		HashMap<String, String> params = new HashMap();
		// params.put("page", "1");
		report = new Report();
		report = reportProvider.getReportByName(reportName, reportMapping);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());

		if (report.getName().equals("topnew")) {
			params.put("soluong", AppConstant.ADMIN_NUM_NEWEST_ITEM + "");
		} else if (report.getName().equals("topsold")) {
			params.put("soluong", AppConstant.ADMIN_NUM_MOST_SOLD_ITEM + "");
		} else if (report.getName().equals("by_user") || report.getName().equals("by_age") ||report.getName().equals("by_product") ) {
			params.put("thang", (calendar.get(Calendar.MONTH) + 1) + "");
		}
		reportProvider.getReport(report, type, params, response);
	}

}